--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved

   FileName: gui.volume

   Created by: petrina.kimura
   Date: 27/11/2012
--]]
------------------------------------------------------------------------
--requires
local tInputApp = require('input')
local tAudioApp = require('audio')
local tPrefenceApp = require('preference')
require('graphic-toolkit.toolkit.guilt')
require('IterateTimer')




------------------------------------------------------------------------
-- Local variables
local tTimeOut = nil
local tMuteTimeOut = nil
local windowVolumeControl = nil
local volumeBox = nil
local circleVolumeBar = nil
local labelVolume = nil
local imageVolumeIcon1 = nil
local imageVolumeIcon2 = nil
local imageVolumeIcon3 = nil
local imageVolumeIcon4 = nil
local windowMute = nil
local muteBox = nil
local imageMuteIcon = nil
local imageMuteBg = nil
-- To verify if the volumeBar is visible
local bIsVolumeVisible = nil
-- Used in case of audio is mute and it is decreasing
local bMute = nil
local nVolume = nil
------------------------------------------------------------------------
-- auxiliar functions
local function resetVolumeWindow()
   windowVolumeControl:reset()
   windowVolumeControl:compose(volumeBox)
end

local function resetMuteWindow()
   windowMute:reset()
   windowMute:compose(muteBox)
end

local function setMuteOpacity(opacity)
   imageMuteIcon:setActivated({opacity = opacity})
   imageMuteBg:setActivated({opacity = opacity})
end

local function volumeUp(bIsRepeat)
	print("entrei no metodo de volume up no arquivo volume")
   local currVolume = tAudioApp.getVolume()
   local nOldVolume = nVolume
   if (bIsRepeat) then
      nVolume = nVolume + 2
   else
      nVolume = nVolume + 1
   end
   if (nVolume > 100) then
      nVolume = 100
   end
   if (bMute) then
      resetVolumeWindow()
      tMuteTimeOut:stop()
      tStackWindow:replace(windowVolumeControl)
      resetMuteWindow()
      tTimeOut:start()
      
      if (nVolume > 0) then
         nVolume = nOldVolume
      end
      
      if (currVolume < 100 and nVolume <= 100 ) then
         tAudioApp.setVolume(nVolume)
      end
      tAudioApp.unmuteAudioUser()
      bMute = false
   else
      if (currVolume < 100 and nVolume <= 100) then
         tAudioApp.setVolume(nVolume)
      end
   end

   if (currVolume < 100) then
      if (nVolume <= 100) then
         if (nVolume == 1) then
            imageVolumeIcon2:setVisible(true)
         elseif (nVolume == 33) then
            imageVolumeIcon3:setVisible(true)
         elseif (nVolume == 80) then
            imageVolumeIcon4:setVisible(true)
         end
      end

      circleVolumeBar:setAngle(nVolume * 3.59)

      if (nVolume == 100 ) then
         circleVolumeBar.visible = false
      end

      labelVolume:setText(tostring(nVolume))
      labelVolume:redraw()

      circleVolumeBar:redraw()
   end

   currVolume = nil
end

local function volumeDown(bIsRepeat)
   if (bMute) then
      tTimeOut:stop()
      resetVolumeWindow()
      tMuteTimeOut:stop()
      tStackWindow:replace(windowVolumeControl)
      resetMuteWindow()
      tTimeOut:start()
      bIsVolumeVisible = true
   end

   if (nVolume > 0) then
      --tAudioApp.volumeDown()
      
      if (bIsRepeat) then
         nVolume = nVolume - 2
      else
         nVolume = nVolume - 1
      end
      if (nVolume < 0) then
         nVolume = 0
      end
      --local nVolume = tAudioApp.getVolume()
      if(not bMute)then
         tAudioApp.setVolume(nVolume)
      end
      
      if (nVolume == 0) then
         imageVolumeIcon2:setVisible(false)
         imageVolumeIcon1:setVisible(true)
      elseif (nVolume == 32) then
         imageVolumeIcon3:setVisible(false)
         imageVolumeIcon2:setVisible(true)
      elseif (nVolume == 79) then
         imageVolumeIcon4:setVisible(false)
         imageVolumeIcon3:setVisible(true)
      end
      circleVolumeBar:setAngle(nVolume * 3.59)

      if (nVolume < 100 ) then
         circleVolumeBar.visible = true
      end

      labelVolume:setText(tostring(nVolume))
      labelVolume:redraw()

      circleVolumeBar:redraw()
   end
end

function muteAudio()
	print("entoru no muteAudio")
	tStackWindow = StackWindow:new()
   --tAudioApp.muteAudioUser()
   bMute = true
   resetMuteWindow()
   tStackWindow:replace(windowMute)
   resetVolumeWindow()
   tTimeOut:stop()
   tMuteTimeOut:start()
end

function unmuteAudio()
   if (nVolume <= 100) then
      --tAudioApp.setVolume(nVolume)
   end
   --tAudioApp.unmuteAudioUser()
   tStackWindow = StackWindow:new()
   bMute = false
   resetVolumeWindow()
   tMuteTimeOut:stop()
   tStackWindow:replace(windowVolumeControl)
   resetMuteWindow()
   tTimeOut:start()
end

-- Synchronous function to create volume GUI.
local function create()
	
   bMute = false

   -- VOLUME WINDOW
   windowVolumeControl = Window:new({
      left = 40,
      top = 293,
      width = 140,
      height = 140
   })
   volumeBox = Box:new({
      width = 140,
      height = 140
   })

   imageVolumeBar = Image:new({fileName = "resources/images/bar-volume.png"})
   imageVolumeBar:setActivated({opacity = 250}) --180
   imageVolumeBar:setAnchors({centerIn = volumeBox})

   imageVolumeShadow = Image:new({fileName = "resources/images/bg-shadow_volume.png"})
   imageVolumeShadow:setActivated({opacity = 250}) --80
   imageVolumeShadow:setAnchors({centerIn = volumeBox})

   imageVolumeinternal = Image:new({z = 3, fileName = "resources/images/bg-internal-volume.png"})
   imageVolumeinternal:setAnchors({centerIn = volumeBox})

   labelVolume = Label:new({z = 4, width = 80, align = "CENTER"})
   labelVolume:setActivated({size = 32, color = {241, 241, 241, 255}})
   
   labelVolume:setText(tostring(tAudioApp.getVolume()))
   
   labelVolume:setAnchors({top = volumeBox.top, horizontalCenter = imageVolumeinternal, topMargin = 37})
   
   nVolume = tAudioApp.getVolume()
  
  
   local tVisibleImages = {true, false, false, false}
	
	
   if (nVolume > 0 and nVolume < 33) then
	
      tVisibleImages = {true, true, false, false}
   
   elseif (nVolume >= 33 and nVolume < 80) then
     
      tVisibleImages = {true, true, true, false}
        
   elseif (nVolume >= 80) then
    
      tVisibleImages = {true, true, false, true}
     
   end
	
   imageVolumeIcon1 = Image:new({z = 4, fileName = "resources/images/ico-volume_00.png", visible = tVisibleImages[1]})
   imageVolumeIcon1:setAnchors({top = volumeBox.top, left = volumeBox.left, topMargin = 77, leftMargin = 58})
   imageVolumeIcon2 = Image:new({z = 4, fileName = "resources/images/ico-volume_01.png", visible = tVisibleImages[2]})
   imageVolumeIcon2:setAnchors({top = volumeBox.top, left = volumeBox.left, topMargin = 77, leftMargin = 58})
   imageVolumeIcon3 = Image:new({z = 4, fileName = "resources/images/ico-volume_02.png", visible = tVisibleImages[3]})
   imageVolumeIcon3:setAnchors({top = volumeBox.top, left = volumeBox.left, topMargin = 77, leftMargin = 58})
   imageVolumeIcon4 = Image:new({z = 4, fileName = "resources/images/ico-volume_03.png", visible = tVisibleImages[4]})
   imageVolumeIcon4:setAnchors({top = volumeBox.top, left = volumeBox.left, topMargin = 77, leftMargin = 58})

   local nInitialAngle = (nVolume * (359 / 100))
   local isBarVisible = true
   if (nVolume == 100) then
      isBarVisible = false
   end
   circleVolumeBar = Circle:new({z = 2, radius = 66.8, angle = nInitialAngle, color = {70, 70, 70, 180}, visible = isBarVisible}) --81, 81, 81, 180
   circleVolumeBar:setAnchors({centerIn = volumeBox})

   volumeBox:add({imageVolumeBar, imageVolumeShadow, circleVolumeBar, imageVolumeinternal, labelVolume, imageVolumeIcon1, imageVolumeIcon2, imageVolumeIcon3, imageVolumeIcon4})
	
   windowVolumeControl:compose(volumeBox)
   -- END VOLUME WINDOW

   -- MUTE WINDOW
   windowMute = Window:new({
      left = 40,
      top = 293,
      width = 140,
      height = 140
   })
   muteBox = Box:new({
      width = 140,
      height = 140
   })

   imageMuteIcon = Image:new({fileName = "resources/images/ico-mute.png"})
   imageMuteIcon:setAnchors({centerIn = muteBox})

   imageMuteBg = Image:new({fileName = "resources/images/bg-mute.png"})
   imageMuteBg:setAnchors({centerIn = muteBox})

   muteBox:add({imageMuteBg, imageMuteIcon})
	print("3")
   windowMute:compose(muteBox)
   -- END MUTE WINDOW
end

-- Synchronous function to show volume GUI.
local function show(sKey)
	
   if (sKey == 'MUTE') then		
      bIsVolumeVisible = false
      muteAudio()
   else
      tTimeOut:start()      
      tStackWindow:push(windowVolumeControl)      
      bIsVolumeVisible = true      
   end
end

-- Synchronous function to hide volume GUI.
local function hide()
   tStackWindow:pop()
end

-- Synchronous function to dispose volume gui.
local function dispose()
   tTimeOut = nil
   tMuteTimeOut = nil
   circleVolumeBar = nil
   labelVolume = nil
   imageVolumeIcon1 = nil
   imageVolumeIcon2 = nil
   imageVolumeIcon3 = nil
   imageVolumeIcon4 = nil
   bMute = nil
   nVolume = nil
   volumeBox = nil
   muteBox = nil
   imageMuteIcon = nil
   imageMuteBg = nil
   
   windowMute:dispose()
   windowVolumeControl:dispose()
   windowMute = nil
   windowVolumeControl = nil
end

------------------------------------------------------------------------
--module table
--module description - This is a template of an GUI Screen module
 tVolumeGui = {}
------------------------------------------------------------------------
--module functions

--- Function called to open this screen, usually calls a local create and a show.
function tVolumeGui.reopen()
   tAudioApp.muteAudioUser()
   --open GUI
   create()
      --start timeout
   local nVolumeTimeout = 2000
   tTimeOut = IterateTimer:new(nVolumeTimeout, tVolumeGui.timeOutFinish, 1)

   tMuteTimeOut = IterateTimer:new(nVolumeTimeout, tVolumeGui.muteTimeOutFinish, 1)
   
   show('MUTE')
   tMuteTimeOut:start()
   nVolumeTimeout = nil
end

--- Function called to open this screen, usually calls a local create and a show.
function tVolumeGui.open(sKey)
tStackWindow = StackWindow:new()
   --open GUI
   create()
      --start timeout
   local nVolumeTimeout = 2000
   tTimeOut = IterateTimer:new(nVolumeTimeout, tVolumeGui.timeOutFinish, 1)
   tMuteTimeOut = IterateTimer:new(nVolumeTimeout, tVolumeGui.muteTimeOutFinish, 1)
   show(sKey)

   nVolumeTimeout = nil
end

---Function called to close this screen, usually calls a local hide and a dispose.
--@param sKey the key used to close this screen, this key is passed to Controller to be checked. (case of open next GUI)
function tVolumeGui.close(sKey)
   --stop timeout
   if (tTimeOut ~= nil) then
      tTimeOut:stop()
      tTimeOut = nil
   end
   --close GUI
   hide()
   dispose()
   --inform controller
end

-- close GUI when finish timeout
function tVolumeGui.timeOutFinish()
   -- decrease volume
   if (bMute) then
      bIsVolumeVisible = false
      resetMuteWindow()
      tStackWindow:replace(windowMute)
      resetVolumeWindow()
      tMuteTimeOut:start()
   else
      bIsVolumeVisible = true
      tVolumeGui.close()
      tVolumeGui.tControllerHandler.close()
   end
end

-- close GUI when finish timeout
function tVolumeGui.muteTimeOutFinish()
   if (not bIsVolumeVisible) then
      setMuteOpacity(200) --50
      resetMuteWindow()
      tStackWindow:replace(windowMute)
      setMuteOpacity(255) --255
   end
end

---Function called cheCk what key was pressed
function tVolumeGui.checkCommand(sKey)
	print("chacando comando")
   if (sKey == 'VOLUME_UP') then
	print("no volume_up do volume")
      bIsVolumeVisible = true
      volumeUp(false)
   elseif (sKey == 'VOLUME_DOWN') then
      --bIsVolumeVisible = true
      volumeDown(false)
   elseif (sKey == 'VOLUME_UP_REP') then
      bIsVolumeVisible = true
      volumeUp(true)
   elseif (sKey == 'VOLUME_DOWN_REP') then
      --bIsVolumeVisible = true
      volumeDown(true)
   elseif (sKey == 'MUTE') then
      --verify if it is already mute
      if (bMute) then
         unmuteAudio()
      else
         bIsVolumeVisible = false
         muteAudio()
      end
   elseif ((sKey ~= 'EXIT') and (sKey ~= 'BACK')) then
      if (not bMute) then
         tVolumeGui.close()
         tVolumeGui.tControllerHandler.close(sKey)
      else
         tVolumeGui.close()
         tVolumeGui.tControllerHandler.tempClose(sKey)
      end
   end
end

---Function to manage keys pressed
--@param sKey the received key
function tVolumeGui.onCommand(sKey)
   --reset timeout
   if (tTimeOut ~= nil) then
      tTimeOut:reset()
   end
   --handle key
   tVolumeGui.checkCommand(sKey)
end

------------------------------------------------------------------------
--handlers to controller
tVolumeGui.tControllerHandler = {}

-- Synchronous function
-- @usage
function tVolumeGui.tControllerHandler.tempClose()
end

-- Synchronous function
-- @usage
function tVolumeGui.tControllerHandler.close(sKey)
end
------------------------------------------------------------------------
return tVolumeGui
